package com.wing.cabinet.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.StorageInventoryInout;
import com.wing.cabinet.model.request.StorageInventoryInoutForm;
import com.wing.cabinet.model.response.StorageInventoryInoutVO;
import com.wing.cabinet.service.StorageInventoryInoutService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-暂存柜存刀/取刀;存/取维修/回收刀管理", tags = "暂存柜存刀/取刀;存/取维修/回收刀管理")
@RestController("appStorageInventoryInoutController")
@RequestMapping("cabinet/app/storageInventoryInout")
public class StorageInventoryInoutController {

    @Autowired
    private StorageInventoryInoutService storageInventoryInoutService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<StorageInventoryInoutVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "暂存记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listTempOrder")
    public PageResult<StorageInventoryInoutVO> listTempOrder(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String code
            ,String cabCode
            ,Long brandId
            ,String startTime
            ,String endTime
            ,String creatorName
            ,Long productTypeId
            ,Long supplierId
    ) {
        Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("status",StorageInventoryInout.Status.created.getValue());
        params.put("operationType",StorageInventoryInout.OperationType.in.getValue());
        params.put("businessType",StorageInventoryInout.BusinessType.temp.getValue());
        params.put("cabCode",cabCode);
        params.put("brandId",brandId);
        params.put("startNotifyTime",startTime);
        params.put("endNotifyTime",endTime);
        params.put("creatorName",creatorName);
        params.put("productTypeId",productTypeId);
        params.put("supplierId",supplierId);
        Page<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "维修回收(待盘验)订单列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listCheckOrder")
    public PageResult<StorageInventoryInoutVO> listCheckOrder(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String code
            ,String cabCode
            ,Long brandId
            ,String startTime
            ,String endTime
    ) {
        Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("status",StorageInventoryInout.Status.created.getValue());
        params.put("operationType",StorageInventoryInout.OperationType.in.getValue());
        params.put("cabCode",cabCode);
        params.put("brandId",brandId);
        params.put("startNotifyTime",startTime);
        params.put("endNotifyTime",endTime);
        params.put("queryFlag","listCheckOrder");
        Page<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "维修订单列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listRepairOrder")
    public PageResult<StorageInventoryInoutVO> listRepairOrder(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String code
            ,StorageInventoryInout.Status status
            ,String cabCode
            ,Long brandId
            ,String startTime
            ,String endTime
            ,String  query
            ,String creatorName
            ,Long productTypeId
            ,Long supplierId
    ) {
        Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (StringUtils.isNotBlank(query)){
            params.put("query",query);
        }
        params.put("code",code);
//        params.put("status",status.getValue());
        params.put("operationType",StorageInventoryInout.OperationType.in.getValue());
        params.put("businessType",StorageInventoryInout.BusinessType.repair.getValue());
        params.put("cabCode",cabCode);
        params.put("creatorName",creatorName);
        params.put("productTypeId",productTypeId);
        params.put("supplierId",supplierId);
        params.put("brandId",brandId);
        params.put("startNotifyTime",startTime);
        params.put("endNotifyTime",endTime);
        Page<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "回收订单列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listRecycleOrder")
    public PageResult<StorageInventoryInoutVO> listRecycleOrder(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String code
            ,StorageInventoryInout.Status status
            ,String cabCode
            ,Long brandId
            ,String startTime
            ,String endTime
            ,String  query
            ,String creatorName
            ,Long productTypeId
            ,Long supplierId
    ) {
        Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (StringUtils.isNotBlank(query)){
            params.put("query",query);
        }
        params.put("code",code);
//        params.put("status",status.getValue());
        params.put("operationType",StorageInventoryInout.OperationType.in.getValue());
        params.put("businessType",StorageInventoryInout.BusinessType.recycle.getValue());
        params.put("cabCode",cabCode);
        params.put("brandId",brandId);
        params.put("startNotifyTime",startTime);
        params.put("endNotifyTime",endTime);
        params.put("creatorName",creatorName);
        params.put("productTypeId",productTypeId);
        params.put("supplierId",supplierId);
        Page<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long storageInventoryInoutId) {
        StorageInventoryInoutVO storageInventoryInoutVO = storageInventoryInoutService.get4appById(storageInventoryInoutId);
        if (storageInventoryInoutVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), storageInventoryInoutVO);
    }

    @PostMapping()
    public JsonResult add(@Validated StorageInventoryInoutForm storageInventoryInoutForm) {
        if (storageInventoryInoutService.add(storageInventoryInoutForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }






}
